﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using Symbonix.NorSoNet.Framework;
using Symbonix.NorSoNet.Security.BLL.Membership;
using Symbonix.Library.Web.Security;
using Symbonix.NorSoNet.Security.BLL;

public partial class MyProfile : MemberBasePage
{
    #region Event
    protected void Page_Load(object sender, EventArgs e)
    {
        responseMessage.Visible = false;
        if (!IsPostBack)
        {
            ddlCountry.DataBind();
            BindGender(rblGender);
            ShowBasicInfoView();
        }
    }
    protected void lbtnEdit_Click(object sender, EventArgs e)
    {
        ShowBasicInfoEdit();
    }
    protected void lnbCancel_Click(object sender, EventArgs e)
    {
        ShowBasicInfoView();
    }
    protected void lnbUpdate_Click(object sender, EventArgs e)
    {
        SystemMembershipFactory membershipFactory = new SystemMembershipFactory();
        IUser member = membershipFactory.GetCurrentUser();
        IUserProfile profile = membershipFactory.GetUserProfile();
        profile = profile.Load(member.Id);

        member.UpdateUserFullName(tbxFirstName.Text, tbxLastName.Text);
        Membership.GetUser(User.Identity.Name);
        if (rblGender.SelectedIndex != -1)
            profile.GenderOption = (Symbonix.NorSoNet.Security.BLL.Options.Gender)Enum.Parse(typeof(Options.Gender), rblGender.SelectedValue);
        profile.Area = tbxArea.Text.Trim();
        profile.City = tbxCity.Text.Trim();
        profile.Country = ddlCountry.SelectedItem.Text;
        profile.ZipCode = tbxZip.Text.Trim();
        if (dpkrDOB.Date.HasValue)
            profile.DateOfBirth = dpkrDOB.Date.Value;

        if (profile.Update() != null)
        {
            responseMessage.SetSuccessMessage("Your profile information updated successfully.");
            ShowBasicInfoView();
        }
        else
        {
            responseMessage.SetFailMessage("Failed to update your profile information, please try again later.");
        }
    }
    #endregion


    private void BindGender(RadioButtonList rbl)
    {
        rbl.Items.Add(new ListItem("Male", Symbonix.NorSoNet.Security.BLL.Options.Gender.Male.ToString()));
        rbl.Items.Add(new ListItem("Female", Symbonix.NorSoNet.Security.BLL.Options.Gender.Female.ToString()));
        rbl.SelectedIndex = -1;
    }
    private void HideAll()
    {
        phdrBasicInfoEdit.Visible = phdrBasicInfoView.Visible = false;
    }
    private void ShowBasicInfoView()
    {
        HideAll();
        phdrBasicInfoView.Visible = true;

        SystemMembershipFactory membershipFactory = new SystemMembershipFactory();
        IUser member = membershipFactory.GetCurrentUser();
        IUserProfile profile = membershipFactory.GetUserProfile().Load(member.Id);

        lblFullName.Text = member.FirstName + " " + member.LastName;
        if (profile == null)
            profile = ModuleManager.MembershipFactory.GetUserProfile().Create(member);
        if (profile != null)
        {
            lblGender.Text = profile.GenderOption.ToString();
            lblDOB.Text = profile.DateOfBirth.ToString("MMMM, dd yyyy");
            lblArea.Text = profile.Area;
            lblCity.Text = profile.City;
            lblZip.Text = profile.ZipCode;
            lblCountry.Text = profile.Country;
        }
    }
    private void ShowBasicInfoEdit()
    {
        HideAll();
        phdrBasicInfoEdit.Visible = true;

        SystemMembershipFactory membershipFactory = new SystemMembershipFactory();
        IUser member = membershipFactory.GetCurrentUser();
        IUserProfile profile = membershipFactory.GetUserProfile().Load(member.Id);

        tbxFirstName.Text = member.FirstName;
        tbxLastName.Text = member.LastName;
        ListItem itemGender = rblGender.Items.FindByValue(profile.GenderOption.ToString());
        if (itemGender != null)
            rblGender.SelectedIndex = rblGender.Items.IndexOf(itemGender);
        dpkrDOB.Date = (profile.DateOfBirth >= System.Data.SqlTypes.SqlDateTime.MinValue ? (DateTime?)profile.DateOfBirth : null);
        tbxArea.Text = profile.Area;
        tbxCity.Text = profile.City;
        tbxZip.Text = profile.ZipCode;
        ListItem itemCountry = ddlCountry.Items.FindByText(profile.Country);
        if (itemCountry != null)
            ddlCountry.SelectedIndex = ddlCountry.Items.IndexOf(itemCountry);
        else
            ddlCountry.SelectedIndex = 0;
    }
    
}
